import React, { useState, useEffect} from 'react';
import { useHistory } from "react-router-dom";
import Btn from "../../components/btn/Btn";
import UserMsg from "../outUser/userMsg/UserMsg"; 
import http from '../../model/http';
import code from "../../model/code";
import './yuyue.less'; 
import {Badge,ActivityIndicator} from 'antd-mobile';

function Yuyue() {
  const [ moreInfo, setMoreInfo ] = useState({}) //客户详细资料
  const [ hadRequest, setHadRequest] = useState(false)
  const [ entryIsHome, setEntryIsHome] = useState(0);  // 页面入口判断
  const [ outUserId, setOutUserId ] = useState({})  
  const [animatingShow,setAnimatingShow] = useState(false)
  const [authority,setAuth ] =useState()

  const getOutUser = async (outUserId)=>{
    const data = await http.post('/customer/checkCustomerBindoutUserId', {outUserId});
    console.log("获取外部联系人情况",data.data)
    // setList(data.data)
    // setIsBindOutUesr(data.isBind)
    if(data.isBind===1){
        // history.push(`/user/details?isBind=${1}&outUserId=${outUserId}&entryIsHome=${entryIsHome}&showHeader=0`, data.data[0]); //跳转到客户详情页面
        return
    }else{
        setHadRequest(true)
        history.push('/user')
    }

  }
   
 


  /*渲染完成*/
  // eslint-disable-next-line react-hooks/exhaustive-deps
  useEffect( async ()=>{
    setAnimatingShow(true)
    // 判断是否绑定
    await code.checkBind() 
    const result =  await code.getContext()

    const data1 = await http.post('/manager/userInfoInit', {});
    console.log("data1",data1)
    if(data1.code == 0){
      setAuth(data1.authority)
    }
    
    if(result=="single_chat_tools"||result=="contact_profile"){
      // 获取外部联系人
      const outUserId = await code.getOutUserId()
      setOutUserId(outUserId)
      
      console.log('outUserId:',outUserId)
      getOutUser(outUserId)
      const data = await http.post('/customer/checkCustomerBindoutUserId', {outUserId});

      console.log('data：',data)
      if(data.isBind===1){ 
        getCustomerDetails(data.data[0].customerCrmId)  //获取客户详细资料
      } 
    }else{
      setHadRequest(true)
      setEntryIsHome(1)
    }

     //获取权限
    

    
  },[])
 


  //获取详细资料
  const getCustomerDetails = (customerCrmId)=>{ 
    
    http.post('/customer/customerDetail',{
      customerCrmId:customerCrmId,
    }).then(res=> {
      console.log('res:',res) 
      setMoreInfo(res.baseInfo[0])  //客户详细资料
      setAnimatingShow(false)
    })
  }
  const history = useHistory();
  const doctor = path =>{
    history.push(path+outUserId,moreInfo); 
  }
  const doctor2 = () =>{ 
    history.push('/yuyue/registrationEdit',{
      outUser:moreInfo
    });
  }
  return (
    
    <div className='v-yuyue-appointment'>
      <div className="body-background-div"></div>
      <UserMsg msg={moreInfo} />

      { authority != 7 ? 
      
      <div className='g-abs g-b0 g-l0 g-w100'>
        <Btn className="v-yuyue-box-btn" type='warning' onClick={()=>{doctor('/yuyue/edit?outUserId=')}}>预约到院面诊</Btn>
        <Btn  type='primary' onClick={()=>{doctor('/yuyue/project?outUserId=')}} className="appointment-box v-yuyue-box-btn">预约手术治疗<Badge className="appointment-Badge" text={'推荐'} corner /></Btn>
        <Btn onClick={()=>{doctor2()}}>手工登记</Btn>
      </div>

      
      : <div></div> }

      
      <ActivityIndicator
        toast
        text="Loading..."
        animating={animatingShow}
      />
    </div>
  )
}

export default Yuyue;

/**
 * age: "29"
 birthday: "1992-03-05"
 customerCrmId: "783567"
 customerIdCardNo: "340323199203053314"
 customerName: "崔子龙"
 dwname: "郝稀"
 mobileNo1: "18325377366"
 mobileNo2: ""
 mobileNo3: ""
 mobileNo4: ""
 mzcode: "MZ00185799"
 sex: "1"
 xcname: "孙晓芳"
 */